package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.utils.NumberFormatUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 文章底部操作栏
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/14 0014 14:12
 */
public class ArticleOperatorLayout extends RelativeLayout {

    private Article mArticle;

    private ImageButton mIbtnBack;

    private ImageView mIvShare;

    private ImageView mIvLikeCount;

    private ImageView mIvCommentCount;

    private TextView mTvConfirm;

    private TextView mTvWriteComment;

    private TextView mTvCommentCount;

    private TextView mTvLikeCount;

    private OnItemClickListener mListener;

    public ArticleOperatorLayout(Context context) {
        this(context, null);
    }

    public ArticleOperatorLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView(context);
        initListeners();
    }

    private void initView(Context context) {
        View rootView = View.inflate(context, R.layout.article_bottom_layout, this);
        mIbtnBack = (ImageButton) rootView.findViewById(R.id.ibtn_back);
        mIvShare = (ImageView) rootView.findViewById(R.id.iv_share);
        mIvLikeCount = (ImageView) rootView.findViewById(R.id.iv_like_count_icon);
        mIvCommentCount = (ImageView) rootView.findViewById(R.id.iv_comment_count_icon);
        mTvConfirm = (TextView) rootView.findViewById(R.id.tv_confirm);
        mTvWriteComment = (TextView) rootView.findViewById(R.id.tv_write_comment);
        mTvCommentCount = (TextView) rootView.findViewById(R.id.tv_comment_count);
        mTvLikeCount = (TextView) rootView.findViewById(R.id.tv_like_count);
    }

    private void initListeners() {
        mIbtnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(POSITION.BACK, mArticle);
                }
            }
        });

        mIvCommentCount.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(POSITION.COMMENT, mArticle);
                }
            }
        });

        mIvShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(POSITION.SHARE, mArticle);
                }
            }
        });

        mTvConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null && mArticle.getExtra() != null) {
                    mListener.onItemClick(POSITION.CONFIRM, mArticle);
                }
            }
        });

        mIvLikeCount.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(POSITION.LIKE, mArticle);
                }
            }
        });

        mTvWriteComment.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(POSITION.COMMENT_NOW, mArticle);
                }
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public void setData(Article article) {
        mArticle = article;
        refreshUI(article);
    }

    private void refreshUI(Article article) {
        String normalCount = NumberFormatUtils.getNormalCountValue(article.getCommentCount());

        if (!TextUtils.isEmpty(normalCount) && !"0".equals(normalCount)) {
            mTvCommentCount.setVisibility(VISIBLE);
            mTvCommentCount.setText(normalCount);
        }

        String likeCount = NumberFormatUtils.getNormalCountValue(article.getVoteCount());
        if (!TextUtils.isEmpty(likeCount) && !"0".equals(likeCount)) {
            mTvLikeCount.setVisibility(VISIBLE);
            mTvLikeCount.setText(likeCount);
        }

        mIvLikeCount.setSelected(article.getHasVote() == 1);

        Article.Extra extra = article.getExtra();

        RelativeLayout.LayoutParams shareIconParams = (LayoutParams) mIvShare.getLayoutParams();
        RelativeLayout.LayoutParams buttonParams = (LayoutParams) mTvConfirm.getLayoutParams();

        if (extra != null) {

            // 活动 --> 没有 “写评论” 框
            mTvConfirm.setEnabled(extra.getEnable() == 1);

            String buttonName = extra.getButtonName();
            // 活动价格
            float enrollMoney = extra.getEnrollMoney();

            if (enrollMoney == 0) {
                // button 宽度 100dp
                buttonParams.width = UIUtils.dp2px(100);
                // 分享按钮距离右边 110dp
                shareIconParams.rightMargin = UIUtils.dp2px(120);

                mTvConfirm.setText(buttonName);
            } else {
                // button 宽度 160dp
                buttonParams.width = UIUtils.dp2px(160);
                // 分享按钮距离右边 170dp
                shareIconParams.rightMargin = UIUtils.dp2px(180);

                showWithMoney(buttonName, enrollMoney);
            }

            mTvConfirm.setLayoutParams(buttonParams);
            mIvShare.setLayoutParams(shareIconParams);

        } else {
            // 非活动(文章/新闻等) --> 有  “写评论” 框
            mTvWriteComment.setVisibility(VISIBLE);
            mTvConfirm.setVisibility(GONE);
            shareIconParams.rightMargin = UIUtils.dp2px(20);
            mIvShare.setLayoutParams(shareIconParams);
        }
    }

    private void showWithMoney(String buttonName, float enrollMoney) {
        SpannableString enroll = TextRenderUtils.getAppearanceText(buttonName + ":", R.style.EnrollStyle);
        SpannableString beautyPrice = TextRenderUtils.getBeautyPrice(enrollMoney + "", null, UIUtils.dp2px(17), UIUtils.dp2px(12));
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(enroll);
        builder.append(beautyPrice);
        mTvConfirm.setText(builder);
    }

    public void notifyDataSetChanged() {
        mIvLikeCount.setSelected(mArticle.getHasVote() == 1);

        String likeCount = NumberFormatUtils.getNormalCountValue(mArticle.getVoteCount());
        if (!TextUtils.isEmpty(likeCount) && !"0".equals(likeCount)) {
            mTvLikeCount.setVisibility(VISIBLE);
            mTvLikeCount.setText(likeCount);
        } else {
            mTvLikeCount.setVisibility(GONE);
        }

    }

    public void cannotEnroll() {
        mTvConfirm.setEnabled(false);
    }

    public interface OnItemClickListener {
        void onItemClick(int position, Article article);
    }

    public interface POSITION {
        int BACK = 0;
        int COMMENT = 1;
        int SHARE = 2;
        int CONFIRM = 3;
        int LIKE = 4;
        int COMMENT_NOW = 5;
    }
}
